//
//  HTClassSetUpView.swift
//  QuitSmokingApp
//
//  Created by   on 2024/3/11.
//

import UIKit
import SnapKit
import Closures

class HTClassSetUpView: UIView {
    lazy var var_titleLabel: UILabel = {
        let var_label = UILabel()
        var_label.numberOfLines = 0
        var_label.textColor = .black
        var_label.textAlignment = .center
        var_label.font = .systemFont(ofSize: 18, weight: .semibold)
        return var_label
    }()
    
    lazy var var_contentLabel: UILabel = {
        let var_lab = UILabel()
        var_lab.numberOfLines = 0
        var_lab.textColor = UIColor("#666666")
        var_lab.textAlignment = .center
        var_lab.font = .systemFont(ofSize: 18, weight: .semibold)
        return var_lab
    }()
    
    lazy var var_closeButton: UIButton = {
        let var_button = UIButton()
        var_button.setImage(UIImage(named: "icon_close-circle-filled"), for: .normal)
        var_button.onTap { [weak self] in
            self?.ht_dismissAnimated()
        }
        return var_button
    }()
    
    
    lazy var var_goButton: UIButton = {
        let var_button = UIButton()
        var_button.setTitle("GO", for: .normal)
        var_button.setTitleColor(.white, for: .normal)
        var_button.backgroundColor = .black
        var_button.titleLabel?.font = .systemFont(ofSize: 20, weight: .semibold)
        var_button.layer.cornerRadius = 8.auto()
        var_button.onTap { [weak self] in
            self?.ht_dismissAnimated()
            self?.BLOCK_goBlock?()
        }
        return var_button
    }()
    
    var BLOCK_goBlock : (() -> Void)?
    
    static func ht_showAnimated(var_title:String, BLOCK_goBlock: (() -> Void)? = nil) {
        let var_view = HTClassSetUpView()
        var_view.var_titleLabel.text = var_title
        var_view.BLOCK_goBlock = BLOCK_goBlock
        var_view.ht_showAnimated()
    }
    
    static func ht_showThemeAnimated(var_title: String, var_content: String) {
        let var_view = HTClassSetUpView()
        var_view.var_titleLabel.text = var_title
        var_view.var_contentLabel.text = var_content
        var_view.var_goButton.isHidden = true
        var_view.var_contentLabel.snp.remakeConstraints { make in
            make.top.equalTo(var_view.var_titleLabel.snp.bottom).offset(10.auto())
            make.left.equalToSuperview().offset(30.auto())
            make.right.equalToSuperview().offset(-30.auto())
            make.bottom.equalToSuperview().offset(-30.auto())
        }
        var_view.ht_showAnimated()
    }
    
    init() {
        super.init(frame: .zero)
        self.backgroundColor = .white
        self.layer.cornerRadius = 16
        self.layer.masksToBounds = true
        ht_setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func ht_setUI() {
        self.addSubview(var_titleLabel)
        self.addSubview(var_contentLabel)
        self.addSubview(var_closeButton)
        self.addSubview(var_goButton)
        
        var_titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(34.auto())
            make.left.equalToSuperview().offset(30.auto())
            make.right.equalToSuperview().offset(-30.auto())
            make.height.greaterThanOrEqualTo(0)
        }
        
        var_contentLabel.snp.makeConstraints { make in
            make.top.equalTo(var_titleLabel.snp.bottom).offset(10.auto())
            make.left.equalToSuperview().offset(30.auto())
            make.right.equalToSuperview().offset(-30.auto())
            make.height.greaterThanOrEqualTo(0)
        }
        
        var_closeButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-2.auto())
            make.top.equalToSuperview().offset(2.auto())
        }
        
        var_goButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(30.auto())
            make.right.equalToSuperview().offset(-30.auto())
            make.top.equalTo(var_contentLabel.snp.bottom).offset(20.auto())
            make.height.equalTo(44.auto())
            make.bottom.equalToSuperview().offset(-20.auto())
        }
    }
    
    func ht_showAnimated() {
        let var_backgroundView = UIView(frame: UIScreen.main.bounds)
        var_backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.4)
        
        let var_maskBtn = UIButton()
        var_maskBtn.backgroundColor = UIColor.clear
        var_maskBtn.addTarget(self, action: #selector(ht_dismissAnimated), for: .touchUpInside)
        var_backgroundView.addSubview(var_maskBtn)
        var_maskBtn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        var_backgroundView.addSubview(self)
        self.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(34)
            make.right.equalToSuperview().offset(-34)
            make.centerY.equalToSuperview()
        }
        self.layoutIfNeeded()
        self.layer.cornerRadius = 16
        
        self.alpha = 0.0;
        self.transform = CGAffineTransformMakeScale(0.1, 0.1);
        
        if let var_mainWindow = HTClassTool.STATIC_keyWindow {
            var_mainWindow.addSubview(var_backgroundView)
            UIView.animate(withDuration: 0.3) {
                self.alpha = 1.0
                self.transform = CGAffineTransformIdentity;
            }
        }
    }
    
    @objc func ht_dismissAnimated() {
        UIView.animate(withDuration: 0.3, animations: {
            self.alpha = 0.0
            self.transform = CGAffineTransformMakeScale(0.1, 0.1);
        }) { finished in
            self.superview?.removeFromSuperview()
        }
    }
}
